import { IAppOption } from "../../../typings";
import { DebtorInfo, User } from "../../../typings/types/mine";
import { loginoutAPI, userProfileAPI } from "../../api/index";
import { countInfoAPI } from "../../api/mine";
import { ZCC } from "../../utils/BasePage";
import { formateDigit, formateMoneyWithUnitW, zcc } from "../../utils/util";

// pages/mine/mine.ts
interface PageData {
  menuItems: object[]
  user?: User
  isLogin: boolean
  debtorInfo?: DebtorInfo
  clueAmount: string
  isShowAddProCover: boolean
  navMaxH: string
}

new ZCC.BasePage({
  handleLogout() {
    if (!this.data.isLogin) {
      wx.reLaunch({
        url: '/pages/login/login'
      });
      return
    }
    const _this = this
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          _this.logout()
        }
      }
    });
  },
  // 
  debtorItemOnClick(e: any) {

    let type = e.currentTarget.dataset.type
    console.log(type)
    switch (type) {
      case "debtor":
        wx.navigateTo({
          url: '/subPackages/packageA/debtorList/debtorList'
          // url: '/subPackages/packageA/litigation/litigation'
        })
        break;
      case "cluenum":
      case "clueamount":
        getApp().globalData.prevPage = "mine";
        wx.switchTab({
          url: '/pages/propertyClue/propertyClue'
        })
        break;

      default: break;
    }
  },
  //
  menuItemOnClick(e: any) {
    console.log(e)
    let index = e.currentTarget.id
    switch (index) {
      case "0":
        wx.navigateTo({
          url: '/pages/mine/subpages/settings/settings?account=' + this.data.user?.nickname
        })
        break;
      case "1":
        wx.navigateTo({
          url: '/pages/mine/subpages/contactus/contactus'
        })
        break;
      case "2":
        this.setData({
          isShowAddProCover: true
        })

        break;

      default: break;
    }

  },
  closeCover() {
    this.setData({
      isShowAddProCover: false
    })
  },
  logout: async function () {
    try {
      const res = await loginoutAPI();
      if (res) {
        // Clear user session data
        zcc.logout()
        this.setData({
          isLogin: false
        })
        wx.reLaunch({
          url: '/pages/login/login'
        });
      } else {
        wx.showToast({
          title: '退出失败',
          icon: 'none'
        });
      }
    } catch (error) {
      wx.showToast({
        title: '服务器开小差了',
        icon: 'none'
      });
    }
  },
  getUserProfile: async function () {

    zcc.getUserProfile({
      loading: true,
      onSuccess: () => {

        this.setData({
          user: getApp<IAppOption>().globalData.userInfo,
          isLogin: getApp<IAppOption>().globalData.isLogin
        })
      }
    })
  },

  getDebtorInfo: async function () {
    try {
      const res = await countInfoAPI();
      if (res.content) {
        const content = res.content
        let clueAmount = content.clueAmount ? formateDigit(formateMoneyWithUnitW(Number(content.clueAmount))) : "-"
        this.setData({
          debtorInfo: content,
          clueAmount: clueAmount
        })
      }
    } finally { }
  },
  // getTabBar 回调设置自定义tab-bar 高亮
  getInstance() {
    if (typeof this.getTabBar === 'function') {
      this.getTabBar((tabBar: any) => {
        tabBar.setData({
          // active: 3
          // hide shiye
          active: 4
        })
      })
    }
  },
  /**
   * 页面的初始数据
   */
  data: <PageData>{
    menuItems: [
      { icon: "/static/images/mine/ic_menu_item_accsettings.png", title: "账号设置" },
      { icon: "/static/images/mine/ic_menu_item_contact.png", title: "联系客服" },
      { icon: "/static/images/mine/ic_menu_item_add.png", title: "添加小程序" },
      { icon: "/static/images/mine/ic_menu_item_share.png", title: "分享小程序" },

    ],
    isLogin: false,
    user: undefined,
    debtorInfo: undefined,
    clueAmount: "-",
    isShowAddProCover: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    console.log(getApp<IAppOption>().globalData.isLogin)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({
      navMaxH: `padding-top: ${getApp<IAppOption>().globalData.safeNavHeight}px`,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getInstance();
    if (getApp<IAppOption>().globalData.isLogin) {
      this.getUserProfile()
      this.getDebtorInfo()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

})